<?php
class kwitansipenjualancontroller extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->model('masterbank_model');
		$this->load->model('subfaktur_model');
		$this->load->model('kwitansimutasicash_model');
		$this->load->model('kwitansimutasibank_model');
		$this->load->model('kwitansimutasigiro_model');
		$this->load->model('kwitansipenjualan_model');
		$this->load->model('mstraccount_model');
		$this->load->library('session');
		$this->load->library('pagination_editable');
		if(($this->session->userdata('fullname') == '') || ($this->session->userdata('fullname') == NULL)){
			redirect('logincontroller');
		}
	}
	
	function index()
	{    	           
		if (isset($_POST['btnview'])) 
		{
            if (isset($_POST['nofaktur'])) 
			{
				$nofaktur = $_POST['nofaktur'];
				$this->kwitansipenjualan_model->setNoFaktur($nofaktur);
				$dataprofile = array(
					'page_title' => 'Finance > kwitansi Pembelian',
					'name' => $this->session->userdata('fullname'),
					'det_barang' => $this->kwitansipenjualan_model->getlistnofakturlike(),
					'bankcombo' => $this->bankCombobox(),
					//'links' => $this->pagination_editable->create_links(),
					'message'=>''
				);  
            }
        } 
		else 
		{			
			$dataprofile = array(
				'page_title' => 'Finance > kwitansi Pembelian',
				'name' => $this->session->userdata('fullname'),
				'det_barang' => $this->kwitansipenjualan_model->getList(),
				'bankcombo' => $this->bankCombobox(),
				//'links' => $this->pagination_editable->create_links(),
				'message'=>''
			);		
		}
		$content = array(
			'content' => 'finance/kwitansi/penjualan/kwitansipenjualan'
		);
		$this->template->load('templates/default',$content,$dataprofile);
	}
	
	function bankCombobox(){
		$data = $this->masterbank_model->getListbank();
		$mycombo = '';
		$mycombo = '<select id="bank" name="bank" onchange="javascript:getBankDetail()">';
		$mycombo .= '<option value="0">Pilih Bank</option>';
		foreach($data as $bank){			
			$mycombo .= '<option value="'.$bank->KodeBank.'">'.$bank->Bankname.'</option>';
		}
		$mycombo .= '</select>';
		return $mycombo;
	}
	
	function getBankDetail(){
		$bankid = $_POST['bankid'];
		$this->masterbank_model->setKodeBank($bankid);
		$resdata = $this->masterbank_model->getBankPerRecord();
		print json_encode($resdata[0]);
	}
	
	function simpan() 
	{		
        if (isset($_POST['btnSave'])) 
		{
			if (isset($_POST['keterangan']) && $_POST['keterangan'] !="" 
				/*or isset($_POST['keteranganbank']) && $_POST['keteranganbank'] !=""
				or isset($_POST['keterangangiro']) && $_POST['keterangangiro'] !=""*/)
			{
				
				$jumdata = count($this->input->post('chkitem'));
				//var_dump($this->input->post('chkitem'));exit;
				$i = 0;
				$ttlamtout=0;
				$ttlqty="";
				$stockstatus="BELI";
				$ttlamt="";
				$ttlfakqty = 0;
				$Qtyall = 0;
				$tglfaktur =date('Y-m-d', strtotime('now'));
				$tanggal=date('Y-m-d H:i:s', strtotime('now'));

				$kodenokwin = $this->kwitansipenjualan_model->getkwitansiCode();
				$this->kwitansipenjualan_model->setNoKwitansi($kodenokwin);
				
				foreach($this->input->post('chkitem') as $iddelay)
				{			
					$kdsubfaktur 		= $_POST['chkitem'][$i];
					$kdfakno	 		= $_POST['kdidfaktur'][$i];
					$kddtlbarang 		= $_POST['kdebrg'][$i];
					$custcode	 		= $_POST['nmacustomer'][$i];
					$quanttity	 		= $_POST['txtQuantity'][$i];
					$disct		 		= 0;
					$amounttranskwin	= -1 * intval($_POST['txtAmount'][$i]);
					$amount		 		= $_POST['txtAmount'][$i];
					$ttlqty		 		= $ttlqty + $quanttity;
					$ttlamt		 		= $ttlamt + $amount;
					
					//var_dump($_POST);exit;
					// insert ke tabel transaksi Kwitansi
					$this->kwitansipenjualan_model->setNoFaktur($kdfakno);
					$this->kwitansipenjualan_model->setQuantity($quanttity);
					$this->kwitansipenjualan_model->setAmount($amounttranskwin);
					
					$this->kwitansipenjualan_model->insertdetail();
	
					// update ke tabel sub faktur product penjualan
					$this->subfaktur_model->setNoSubFaktur($kdsubfaktur);
					$this->subfaktur_model->setStatus("LUNAS");
					/*$Qtyall = $this->subfaktur_model->getTotalqty();
					
					if($Qtyall == $ttlqty)
					{		*/	
						$this->subfaktur_model->deletesubfaktur();
					/*}
					else
					{
						$ttlfakqty = $Qtyall - $ttlqty;
						$this->transaksibankpembelian_model->setTotalQuantity($ttlfakqty);
						$this->transaksibankpembelian_model->editquantheader();
						$this->transaksibankpembelian_model->deletefakturpembeliandetial();
					}*/
					
					$i++;
				}
							
				if ($_POST['typebyr']=="CASH")
				{	
					// insert ke tabel Cash
					$this->kwitansimutasicash_model->setNoKwitansi($kodenokwin);
					$this->kwitansimutasicash_model->setNoFaktur("-");
					$this->kwitansimutasicash_model->setTotalQuantity($ttlqty);
					$this->kwitansimutasicash_model->setDiscount("0");
					$this->kwitansimutasicash_model->setTotalAmount_In($_POST['ttlamount']);
					$this->kwitansimutasicash_model->setTotalAmount_Out($ttlamtout);
					$this->kwitansimutasicash_model->setKeterangan($_POST['keterangan']);
					$this->kwitansimutasicash_model->setCreatedBy($this->session->userdata('userid'));
					$this->kwitansimutasicash_model->setCreatedOn($tanggal);
					
					$this->kwitansimutasicash_model->insert();
				}
				elseif ($_POST['typebyr']=="BANK")
				{
					// insert ke tabel mutasi bank
					$this->kwitansimutasibank_model->setNoKwitansi($kodenokwin);
					$this->kwitansimutasibank_model->setNoFaktur("-");
					$this->kwitansimutasibank_model->setTotalQuantity($ttlqty);
					$this->kwitansimutasibank_model->setDiscount("0");
					$this->kwitansimutasibank_model->setTotalAmount_In($_POST['ttlamountbank']);
					$this->kwitansimutasibank_model->setTotalAmount_Out($ttlamtout);
					$this->kwitansimutasibank_model->setKodeBank($_POST['bank']);
					$this->kwitansimutasibank_model->setKeterangan($_POST['keterangan']);
					$this->kwitansimutasibank_model->setCreatedBy($this->session->userdata('userid'));
					$this->kwitansimutasibank_model->setCreatedOn($tanggal);
					$this->kwitansimutasibank_model->setActive("1");
					
					$this->kwitansimutasibank_model->insert();
				}
				elseif ($_POST['typebyr']=="GIRO")
				{
					// insert ke tabel mutasi Giro
					
					$this->kwitansimutasigiro_model->setNoKwitansi($kodenokwin);
					$this->kwitansimutasigiro_model->setNoFaktur("-");
					$this->kwitansimutasigiro_model->setTotalQuantity($ttlqty);			
					$this->kwitansimutasigiro_model->setTotalAmount_In($_POST['ttlamountgiro']);
					$this->kwitansimutasigiro_model->setTotalAmount_Out($ttlamtout);
					$this->kwitansimutasigiro_model->setBankname($_POST['nmbankgiro']);
					$this->kwitansimutasigiro_model->setNogiro($_POST['nogiro']);
					$this->kwitansimutasigiro_model->setTanggalterbit($_POST['tglterbit']);
					$this->kwitansimutasigiro_model->setTanggalcair($_POST['tglcair']);
					$this->kwitansimutasigiro_model->setKeterangan($_POST['keterangan']);
					$this->kwitansimutasigiro_model->setCreatedBy($this->session->userdata('userid'));
					$this->kwitansimutasigiro_model->setCreatedOn($tanggal);
					$this->kwitansimutasigiro_model->setActive("1");
					
					$this->kwitansimutasigiro_model->insert();
				}
				
										
				// insert ke tabel kwitansi
				$this->kwitansipenjualan_model->setnamapelanggan($custcode);
				$this->kwitansipenjualan_model->setTotalQuantity($ttlqty);
				$this->kwitansipenjualan_model->setTypebayar($_POST['typebyr']);
				if ($_POST['typebyr']=="CASH")
				{
					$ttlamountkwitansicash	= -1 * intval($_POST['ttlamount']);
					$this->kwitansipenjualan_model->setTotalAmount($ttlamountkwitansicash);
					$this->kwitansipenjualan_model->setKodeBank('-');
					$this->kwitansipenjualan_model->setBanknamepelanggan('-');
					$this->kwitansipenjualan_model->setNoRekeningpelanggan('-');
					$this->kwitansipenjualan_model->setKeterangan($_POST['keterangan']);
					//var_dump($ttlamountkwitansicash);exit;
				}
				elseif ($_POST['typebyr']=="BANK")
				{
					$ttlamountkwintansibank	= -1 * intval($_POST['ttlamountbank']);
					// update ke tabel Account bank
					$this->mstraccount_model->setKodeBank($_POST['bank']);
					
					$stockArray = $this->mstraccount_model->gettotalamount();
					
					$saldoawl = $stockArray[0]->SaldoAwal;
					$uangmsk = $stockArray[0]->Uangmasuk;
					$uangout = $stockArray[0]->Uangkeluar;
					$sldakhr = $stockArray[0]->SaldoAkhir;
					
					$ttlsldawal = $saldoawl;
					$ttluangmsk = $uangmsk + $_POST['ttlamountbank'];
					$ttluangout = $uangout ;
					$ttlsldakhr = $sldakhr + $_POST['ttlamountbank'];
					
					$this->mstraccount_model->setSaldoAwal($ttlsldawal);
					$this->mstraccount_model->setUangmasuk($ttluangmsk);
					$this->mstraccount_model->setUangkeluar($ttluangout);
					$this->mstraccount_model->setSaldoAkhir($ttlsldakhr);
					$this->mstraccount_model->setLastupdateby($this->session->userdata('userid'));
					$this->mstraccount_model->setLastUpdateOn($tanggal);
					
					$this->mstraccount_model->ubahmstraccount();
					// sampe sini
					
					$this->kwitansipenjualan_model->setTotalAmount($ttlamountkwintansibank);
					$this->kwitansipenjualan_model->setKodeBank($_POST['bank']);
					$this->kwitansipenjualan_model->setBanknamepelanggan($_POST['nmbank']);
					$this->kwitansipenjualan_model->setNoRekeningpelanggan($_POST['norekcust']);
					$this->kwitansipenjualan_model->setKeterangan($_POST['keterangan']);
				}
				elseif ($_POST['typebyr']=="GIRO")
				{
					$ttlamountkwitansigiro	= -1 * intval($_POST['ttlamountgiro']);
					$this->kwitansipenjualan_model->setTotalAmount($ttlamountkwitansigiro);
					$this->kwitansipenjualan_model->setKodeBank('-');
					$this->kwitansipenjualan_model->setBanknamepelanggan('-');
					$this->kwitansipenjualan_model->setNoRekeningpelanggan('-');
					$this->kwitansipenjualan_model->setKeterangan($_POST['keterangan']);
				}

				$this->kwitansipenjualan_model->setCreatedBy($this->session->userdata('userid'));
				$this->kwitansipenjualan_model->setCreatedOn($tanggal);
				$this->kwitansipenjualan_model->setLastUpdateBy($this->session->userdata('userid'));
				$this->kwitansipenjualan_model->setLastUpdateOn($tanggal);
				$this->kwitansipenjualan_model->insert();				
				
				$dataprofile = array(
					'page_title' => 'Finance > Pembayaran Pembelian Barang',
					'name' => $this->session->userdata('fullname'),
					'kodekwitansino'=> $this->kwitansipenjualan_model->ambilnokwitansi1(),
					'transkwitansi' => $this->kwitansipenjualan_model->getListfakturkwt()	
				);		
				$content = array(
					'content' => 'finance/kwitansi/penjualan/formkwitansipenjualan'
				);
				$this->template->load('templates/default',$content,$dataprofile);
				
				//redirect('kwitansipembeliancontroller');
			}
			else
			{
				$dataprofile = array(
					'page_title' => 'Finance > kwitansi Penjualan',
					'name' => $this->session->userdata('fullname'),
					//'mstrfaktur' => $this->kwitansipenjualan_model->ambilnofaktur(),
					//'masterbank' => $this->masterbank_model->getListbank(),
					'bankcombo' => $this->bankCombobox(),
					'message' => '<div style="color:red;font-size:9pt;font-weight:bold;">Keterangan Belum di isi </div>',
					'det_barang' => $this->kwitansipenjualan_model->getList()
					//'links' => $this->pagination_editable->create_links()
                );
                $content = array(
                    'content' => 'finance/kwitansi/penjualan/kwitansipenjualan'
                );
                $this->template->load('templates/default', $content, $dataprofile);
			}

        } 
		else 
		{
            $dataprofile = array(
				'page_title' => 'Finance > kwitansi Pembelian',
				'name' => $this->session->userdata('fullname'),
				//'mstrfaktur' => $this->kwitansipenjualan_model->ambilnofaktur(),
				//'masterbank' => $this->masterbank_model->getListbank(),
				'bankcombo' => $this->bankCombobox(),
				'det_barang' => $this->kwitansipenjualan_model->getList(),
				//'links' => $this->pagination_editable->create_links(),
				'message'=>''	
			);	
			
			$content = array(
				'content' => 'finance/kwitansi/penjualan/kwitansipenjualan'
			);
			$this->template->load('templates/default',$content,$dataprofile);		
		}
    }
}